<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>小说智能分行工具</title>
    <style>
        :root {
            --bg-color: #f0f2f5;
            --container-bg: #ffffff;
            --text-color: #333;
            --primary-color: #007bff;
            --primary-hover-color: #0056b3;
            --border-color: #dcdfe6;
            --box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            background-color: var(--bg-color);
            color: var(--text-color);
            margin: 0;
            padding: 2rem;
        }

        .container {
            max-width: 1800px; /* 适配大屏幕 */
            margin: 0 auto;
            background-color: var(--container-bg);
            border-radius: 12px;
            padding: 2rem 3rem;
            box-shadow: var(--box-shadow);
        }

        h1 {
            text-align: center;
            color: var(--primary-color);
            margin-bottom: 2rem;
            font-size: 2.5rem;
        }

        .formatter-area {
            display: flex;
            gap: 2rem;
            width: 100%;
        }

        .box {
            flex: 1;
            display: flex;
            flex-direction: column;
        }

        label {
            font-size: 1.2rem;
            font-weight: 600;
            margin-bottom: 0.8rem;
        }

        textarea {
            width: 100%;
            height: 65vh; /* 视窗高度的65% */
            padding: 1rem;
            border: 1px solid var(--border-color);
            border-radius: 8px;
            font-size: 16px;
            line-height: 1.6;
            resize: vertical;
            box-sizing: border-box; /* 确保 padding 不会影响宽度 */
            transition: border-color 0.3s, box-shadow 0.3s;
        }

        textarea:focus {
            outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.25);
        }

        .controls {
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 1.5rem;
            margin-top: 2rem;
        }

        .btn {
            padding: 0.8rem 2rem;
            font-size: 1.1rem;
            font-weight: 600;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            transition: background-color 0.3s, transform 0.1s;
        }

        .btn-primary {
            background-color: var(--primary-color);
            color: white;
        }

        .btn-primary:hover {
            background-color: var(--primary-hover-color);
        }

        .btn:active {
            transform: scale(0.98);
        }

        .btn:disabled {
            background-color: #a0c3ff;
            cursor: not-allowed;
        }

        .btn-secondary {
            background-color: #6c757d;
            color: white;
        }

        .btn-secondary:disabled {
            background-color: #b0b5b9;
        }

        .btn-secondary:hover:not(:disabled) {
            background-color: #5a6268;
        }

        /* 响应式设计，当屏幕宽度小于1200px时，垂直排列 */
        @media (max-width: 1200px) {
            .formatter-area {
                flex-direction: column;
            }
            .container {
                padding: 1rem 1.5rem;
            }
            h1 {
                font-size: 2rem;
            }
        }
    </style>
</head>
<body>

<div class="container">
    <h1>小说智能分行工具</h1>
    <div class="formatter-area">
        <div class="box">
            <label for="originalText">小说原文</label>
            <textarea id="originalText" placeholder="在此粘贴小说原文..."></textarea>
        </div>
        <div class="box">
            <label for="formattedText">处理结果</label>
            <textarea id="formattedText" readonly placeholder="处理后的文本将显示在这里..."></textarea>
        </div>
    </div>
    <div class="controls">
        <button id="formatButton" class="btn btn-primary">开始处理</button>
        <button id="downloadButton" class="btn btn-secondary" disabled>下载结果 (.txt)</button>
    </div>
</div>

<script>
    const originalTextArea = document.getElementById('originalText');
    const formattedTextArea = document.getElementById('formattedText');
    const formatButton = document.getElementById('formatButton');
    const downloadButton = document.getElementById('downloadButton');

    // 处理按钮点击事件
    formatButton.addEventListener('click', async () => {
        const originalText = originalTextArea.value;
        if (!originalText.trim()) {
            alert('请输入小说原文！');
            return;
        }

        // 设置加载状态
        formatButton.disabled = true;
        formatButton.textContent = '正在处理中...';
        downloadButton.disabled = true;
        formattedTextArea.value = '';

        try {
            const response = await fetch('http://parser-api.anmong.com/api/format', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ text: originalText })
            });

            if (!response.ok) {
                throw new Error(`服务器错误: ${response.status} ${response.statusText}`);
            }

            const data = await response.json();
            formattedTextArea.value = data.formattedText;
            // 如果有结果，则启用下载按钮
            if (data.formattedText) {
                downloadButton.disabled = false;
            }

        } catch (error) {
            console.error('处理失败:', error);
            alert(`处理失败，请检查网络连接或查看控制台错误信息。\n${error.message}`);
            formattedTextArea.value = '处理时发生错误。';
        } finally {
            // 恢复按钮状态
            formatButton.disabled = false;
            formatButton.textContent = '开始处理';
        }
    });

    // 下载按钮点击事件
    downloadButton.addEventListener('click', () => {
        const textToDownload = formattedTextArea.value;
        if (!textToDownload) {
            return;
        }

        // 创建一个 Blob 对象
        const blob = new Blob([textToDownload], { type: 'text/plain;charset=utf-8' });

        // 创建一个隐藏的 <a> 标签用于下载
        const link = document.createElement('a');
        link.href = URL.createObjectURL(blob);
        link.download = 'formatted_novel.txt';

        // 触发点击
        document.body.appendChild(link);
        link.click();

        // 清理
        document.body.removeChild(link);
        URL.revokeObjectURL(link.href);
    });
</script>

</body>
</html>